package com.itbaizhan.dao.impl;

import co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
import co.elastic.clients.elasticsearch.core.search.HighlighterTagsSchema;
import com.itbaizhan.dao.ItemESDao;
import com.itbaizhan.pojo.ItemES;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.client.elc.NativeQuery;
import org.springframework.data.elasticsearch.client.elc.NativeQueryBuilder;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.HighlightQuery;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;
import org.springframework.data.elasticsearch.core.query.highlight.Highlight;
import org.springframework.data.elasticsearch.core.query.highlight.HighlightField;
import org.springframework.data.elasticsearch.core.query.highlight.HighlightFieldParameters;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @author spf
 * @date 2024/3/4
 * @time 20:53
 */
@Repository
public class ItemESDaoImpl implements ItemESDao {

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @Value("${livegoods.search.init.enable}")
    private Boolean initEnabled = false;

    @Override
    public void batchIndex(List<ItemES> items) {
        //判断是否需要初始化索引
        if (initEnabled) {
            createIndex();
        }

        ArrayList<IndexQuery> list = new ArrayList();
        for (ItemES item : items) {
            list.add(new IndexQueryBuilder().withObject(item).build());
        }
        //批量插入数据
        elasticsearchTemplate.bulkIndex(list, ItemES.class);
    }

    @Override
    public List<ItemES> queryForPage(String city, String content, int page, int rows) {
        // 高亮设置
//        HighlightField field = new HighlightField("title");
        // 查询条件
        BoolQuery.Builder builder = QueryBuilders.bool()
                .must(QueryBuilders.multiMatch(builder1 -> builder1.fields("title", "houseType", "rentType").query(content)))
                .must(QueryBuilders.match(builder1 -> builder1.field("city").query(city)));
        NativeQuery nativeQuery = new NativeQueryBuilder()
                .withQuery(builder.build()._toQuery())
//                .withHighlightQuery()
                .withPageable(PageRequest.of(page, rows))
                .build();

        SearchHits<ItemES> search = elasticsearchTemplate.search(nativeQuery, ItemES.class);
        List<SearchHit<ItemES>> searchHits = search.getSearchHits();
        ArrayList<ItemES> arrayList = new ArrayList<>();
        for (SearchHit<ItemES> searchHit : searchHits) {
            ItemES itemES = new ItemES();
            itemES.setId(searchHit.getContent().getId());
            itemES.setTitle(searchHit.getContent().getTitle());
            itemES.setCity(searchHit.getContent().getCity());
            itemES.setPrice(searchHit.getContent().getPrice());
            itemES.setHouseType(searchHit.getContent().getHouseType());
            itemES.setRentType(searchHit.getContent().getRentType());
            itemES.setImg(searchHit.getContent().getImg());
            arrayList.add(itemES);
        }
        return arrayList;
    }

    private void createIndex() {
        IndexOperations indexOps = elasticsearchTemplate.indexOps(ItemES.class);
        if (indexOps.exists()) {
            indexOps.delete();
        } else {
            indexOps.create();
            indexOps.refresh();
        }
    }
}
